//
//  InterViewSkilDetailVC.m
//  
//
//  Created by apple on 16/3/9.
//
//

#import "InterViewSkilDetailVC.h"
#import "InterViewSkillAPIManger.h"
#define IS_IOS_7 [[UIDevice currentDevice].systemVersion doubleValue] >= 7.0
static NSString *const kCellWithIdentifier = @"cellId";

@interface InterViewSkilDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) InterViewSkillAPIManger *interViewSkillAPIManger;

@property (nonatomic, strong) NSArray *dataList;

@property (nonatomic, strong) NSMutableArray *titleNames;
- (void)initializeDataSource;     /**< 初始化数据源 */
- (void)initializeUserIntefcace;  /**< 初始化用户界面 */
@property (weak, nonatomic) IBOutlet UITableView *tablView;


@end

@implementation InterViewSkilDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserIntefcace];
}
- (void)initializeDataSource{
    
    NSDictionary *paprameter = @{@"techniquesId":self.techniquesId};
    [self.interViewSkillAPIManger startInterViewSkillDetailRequestWithPaprameters:paprameter responds:^(RespondsStatus status, NSString *message, id reslutData) {
        self.dataList = reslutData;
//        [self.tablView reloadData];
        self.titleNames = [NSMutableArray array];
        for (int  i = 0; i < _dataList.count; i++) {
            NSString *titleName = _dataList[i][@"techniquesExtTitle"];
            [self.titleNames addObject:titleName];
        }
        [self.tablView reloadData];
    }];
   
    
}
- (void)initializeUserIntefcace{
    self.navigationItem.title = self.techniquesTitle;
}
#pragma mark - private
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font containsSize:(CGSize)containsSize {
    CGSize size = CGSizeZero;
    CGRect rect = [string boundingRectWithSize:containsSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:font} context:nil];
    size = rect.size;
    return size;
}
- (float)heightForString:(NSString *)value fontSize:(UIFont *)font andWidth:(float)width{
    
    if (IS_IOS_7) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGRect rectToFit = [value boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
        return rectToFit.size.height;
    }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
        CGSize sizeToFit = [value sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
#pragma clang diagnostic pop
        return sizeToFit.height;
        
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _titleNames.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellWithIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellWithIdentifier];
    }
    cell.textLabel.frame = cell.frame;
    cell.textLabel.text = _dataList[indexPath.section][@"techniquesExtContent"];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    CGSize size = [self sizeWithString:_dataList[indexPath.row][@"techniquesExtContent"] font:[UIFont systemFontOfSize:20] containsSize:CGSizeMake(self.tablView.bounds.size.width, CGFLOAT_MAX)];
    
    CGFloat cell_height = [self heightForString:_dataList[indexPath.section][@"techniquesExtContent"] fontSize:[UIFont systemFontOfSize:20] andWidth:self.tablView.bounds.size.width];
    return cell_height;
}

//自定义头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 40)];
    label.font = [UIFont boldSystemFontOfSize:20];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = _titleNames[section];
    label.textColor = [UIColor brownColor];
    return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
#pragma mark - getters
- (InterViewSkillAPIManger *)interViewSkillAPIManger{
    if (!_interViewSkillAPIManger) {
        _interViewSkillAPIManger = [[InterViewSkillAPIManger alloc] init];
    }
    return _interViewSkillAPIManger;
}

@end
